package net.jalbum.filters;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

import se.datadosen.jalbum.Msg;

import net.jalbum.filterManager.FilterProperties.FilterCategory;

public class TextFilterPlugin extends MultipleResizableBoxPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = -2877909947583115228L;
	protected TextControl textControl;
	protected TextBox textBox = new TextBox();

	public TextFilterPlugin() {
		this.name = Msg.getString(this, "filters.text.name");
		this.shortName = Msg.getString(this, "filters.text.shortName");
		this.icon = new ImageIcon(this.getClass().getResource("res/text.png"));
		this.author = "David Fichtmueller";
		this.version = "1.0";
		this.description = Msg.getString(this, "filters.text.description");
		this.category = FilterCategory.BASIC;
		this.prescale = false;
		this.postscale = true;

		this.namePrefix = "Text Box ";

		this.textControl = new TextControl(this);

		this.multipleResizableObjectControl = this.textControl;
		this.multipleResizableBoxControl = this.textControl;
		this.resizableObjectControl = this.textControl;
		this.emptyClickCreatesNewObject = true;
	}

	@Override
	public void init() {
		this.textBox = new TextBox();
		this.textBox.setName(this.namePrefix + (this.counter));
		this.textBox.setParentPanel(this);

		this.objectVector.clear();
		this.objectVector.add(this.textBox);

		this.resizableBox = this.textBox;
		this.resizableObject = this.resizableBox;
		this.hoveredOverObject = this.resizableObject;
	}

	@Override
	public JPanel getControls() {
		return this.textControl;
	}

	@Override
	public void addObject() {
		this.resizableObject.setState(ResizableObject.NOTHING);

		this.textBox = new TextBox(this.width, this.height);
		this.textBox.setName(this.namePrefix + (this.counter - 1));
		this.textBox.setParentPanel(this);
		this.textBox.setInitial(true);
		// new textbox is added on top of the others
		this.objectVector.add(0, this.textBox);
		this.resizableBox = this.textBox;
		this.resizableObject = this.textBox;
		this.multipleResizableObjectControl.adjustResizableObject(this.resizableObject);
		this.counter++;
		this.renderPreview();
	}

	// public void setTextControl(final TextControl textBoxControl) {
	// this.textControl = textBoxControl;
	// }

	public TextBox getTextBox() {
		return (TextBox) (this.resizableObject);
	}

	@Override
	public void paint(final Graphics g) {
		// draw Text on it
		for (int i = this.objectVector.size() - 1; i >= 0; i--) {
			if (this.objectVector.get(i) == this.resizableObject && this.outlineSelected) {
				((TextBox) (this.resizableObject)).paintSelected(g);
			} else {
				this.objectVector.get(i).paint(g);
			}
		}
	}

	@Override
	public void renderPreview() {
		this.outputImage = this.cloneBufferedImage(this.inputImage);
		this.paint(this.outputImage.getGraphics());
		this.filterManager.setGUIImage(this.outputImage);
	}

	@Override
	public BufferedImage renderImage(final BufferedImage bi) {
		// final image
		final Graphics g = bi.getGraphics();

		// draw Text on it
		for (int i = this.objectVector.size() - 1; i >= 0; i--) {
			((TextBox) this.objectVector.get(i)).setHeight(bi.getHeight());
			((TextBox) this.objectVector.get(i)).setWidth(bi.getWidth());
			((TextBox) this.objectVector.get(i)).prepareTextDrawing(g, true);
			((TextBox) this.objectVector.get(i)).drawText(g);
		}
		return bi;
	}

	// public TextControl getTextControl() {
	// return this.textControl;
	// }

	public void setTextBox(final TextBox textBox) {
		this.textBox = textBox;
	}

	@Override
	public void setPreviewImage(final BufferedImage bi) {
		super.setPreviewImage(bi);

		// set the values from the current textbox
		this.textControl.adjustTextBox(this.textBox);
	}

	@Override
	public void dispose() {
		super.dispose();
		this.textControl.dispose();
	}

}
